{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 86,
   "source": [
    "import pandas as pd\r\n",
    "import numpy as np\r\n",
    "from sklearn.model_selection import cross_val_score\r\n",
    "from sklearn.model_selection import train_test_split\r\n",
    "from sklearn.ensemble import RandomForestClassifier\r\n",
    "from sklearn.metrics import accuracy_score\r\n",
    "from sklearn.model_selection import GridSearchCV\r\n",
    "import time"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "pd.read_csv('Data/Phishing_Infogain_test.csv')"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>domain_token_count</th>\n",
       "      <th>avgdomaintokenlen</th>\n",
       "      <th>tld</th>\n",
       "      <th>domainlength</th>\n",
       "      <th>pathurlRatio</th>\n",
       "      <th>argDomanRatio</th>\n",
       "      <th>domainUrlRatio</th>\n",
       "      <th>pathDomainRatio</th>\n",
       "      <th>NumberofDotsinURL</th>\n",
       "      <th>CharacterContinuityRate</th>\n",
       "      <th>host_letter_count</th>\n",
       "      <th>LongestPathTokenLength</th>\n",
       "      <th>SymbolCount_Domain</th>\n",
       "      <th>Entropy_Domain</th>\n",
       "      <th>class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>5.50</td>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.17</td>\n",
       "      <td>0.15</td>\n",
       "      <td>5.08</td>\n",
       "      <td>1</td>\n",
       "      <td>0.75</td>\n",
       "      <td>11</td>\n",
       "      <td>48</td>\n",
       "      <td>1</td>\n",
       "      <td>0.86</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>5.00</td>\n",
       "      <td>3</td>\n",
       "      <td>17</td>\n",
       "      <td>0.69</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.22</td>\n",
       "      <td>3.18</td>\n",
       "      <td>3</td>\n",
       "      <td>0.65</td>\n",
       "      <td>15</td>\n",
       "      <td>40</td>\n",
       "      <td>2</td>\n",
       "      <td>0.78</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>4.00</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.78</td>\n",
       "      <td>0.13</td>\n",
       "      <td>6.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0.67</td>\n",
       "      <td>8</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>4.50</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>0.73</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.16</td>\n",
       "      <td>4.70</td>\n",
       "      <td>1</td>\n",
       "      <td>0.80</td>\n",
       "      <td>9</td>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "      <td>0.88</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>6.00</td>\n",
       "      <td>2</td>\n",
       "      <td>13</td>\n",
       "      <td>0.71</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.19</td>\n",
       "      <td>3.69</td>\n",
       "      <td>2</td>\n",
       "      <td>0.77</td>\n",
       "      <td>12</td>\n",
       "      <td>39</td>\n",
       "      <td>1</td>\n",
       "      <td>0.83</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15362</th>\n",
       "      <td>2</td>\n",
       "      <td>8.00</td>\n",
       "      <td>2</td>\n",
       "      <td>17</td>\n",
       "      <td>0.35</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.46</td>\n",
       "      <td>0.76</td>\n",
       "      <td>1</td>\n",
       "      <td>0.82</td>\n",
       "      <td>16</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>0.88</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15363</th>\n",
       "      <td>3</td>\n",
       "      <td>9.00</td>\n",
       "      <td>3</td>\n",
       "      <td>29</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.07</td>\n",
       "      <td>0.78</td>\n",
       "      <td>0.03</td>\n",
       "      <td>2</td>\n",
       "      <td>0.59</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.81</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15364</th>\n",
       "      <td>3</td>\n",
       "      <td>6.67</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "      <td>0.22</td>\n",
       "      <td>0.09</td>\n",
       "      <td>0.59</td>\n",
       "      <td>0.36</td>\n",
       "      <td>2</td>\n",
       "      <td>0.50</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>0.80</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15365</th>\n",
       "      <td>2</td>\n",
       "      <td>8.00</td>\n",
       "      <td>2</td>\n",
       "      <td>17</td>\n",
       "      <td>0.35</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.46</td>\n",
       "      <td>0.76</td>\n",
       "      <td>1</td>\n",
       "      <td>0.82</td>\n",
       "      <td>16</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0.79</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15366</th>\n",
       "      <td>2</td>\n",
       "      <td>9.00</td>\n",
       "      <td>2</td>\n",
       "      <td>19</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.51</td>\n",
       "      <td>0.58</td>\n",
       "      <td>2</td>\n",
       "      <td>0.84</td>\n",
       "      <td>18</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0.81</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>15367 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       domain_token_count  avgdomaintokenlen  tld  domainlength  pathurlRatio  \\\n",
       "0                       2               5.50    2            12          0.76   \n",
       "1                       3               5.00    3            17          0.69   \n",
       "2                       2               4.00    2             9          0.76   \n",
       "3                       2               4.50    2            10          0.73   \n",
       "4                       2               6.00    2            13          0.71   \n",
       "...                   ...                ...  ...           ...           ...   \n",
       "15362                   2               8.00    2            17          0.35   \n",
       "15363                   3               9.00    3            29          0.03   \n",
       "15364                   3               6.67    3            22          0.22   \n",
       "15365                   2               8.00    2            17          0.35   \n",
       "15366                   2               9.00    2            19          0.30   \n",
       "\n",
       "       argDomanRatio  domainUrlRatio  pathDomainRatio  NumberofDotsinURL  \\\n",
       "0               0.17            0.15             5.08                  1   \n",
       "1               0.12            0.22             3.18                  3   \n",
       "2               0.78            0.13             6.00                  1   \n",
       "3               0.20            0.16             4.70                  1   \n",
       "4               2.69            0.19             3.69                  2   \n",
       "...              ...             ...              ...                ...   \n",
       "15362           0.12            0.46             0.76                  1   \n",
       "15363           0.07            0.78             0.03                  2   \n",
       "15364           0.09            0.59             0.36                  2   \n",
       "15365           0.12            0.46             0.76                  1   \n",
       "15366           0.11            0.51             0.58                  2   \n",
       "\n",
       "       CharacterContinuityRate  host_letter_count  LongestPathTokenLength  \\\n",
       "0                         0.75                 11                      48   \n",
       "1                         0.65                 15                      40   \n",
       "2                         0.67                  8                      22   \n",
       "3                         0.80                  9                      33   \n",
       "4                         0.77                 12                      39   \n",
       "...                        ...                ...                     ...   \n",
       "15362                     0.82                 16                       8   \n",
       "15363                     0.59                 27                       0   \n",
       "15364                     0.50                 20                       6   \n",
       "15365                     0.82                 16                       6   \n",
       "15366                     0.84                 18                       5   \n",
       "\n",
       "       SymbolCount_Domain  Entropy_Domain  class  \n",
       "0                       1            0.86    0.0  \n",
       "1                       2            0.78    0.0  \n",
       "2                       1            1.00    0.0  \n",
       "3                       1            0.88    0.0  \n",
       "4                       1            0.83    0.0  \n",
       "...                   ...             ...    ...  \n",
       "15362                   1            0.88    1.0  \n",
       "15363                   2            0.81    1.0  \n",
       "15364                   2            0.80    1.0  \n",
       "15365                   1            0.79    1.0  \n",
       "15366                   1            0.81    1.0  \n",
       "\n",
       "[15367 rows x 15 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "def dataload(filename):\r\n",
    "    df = pd.read_csv(filename)\r\n",
    "    df = df.replace('benign', '1').replace('Defacement', '0').replace('?', '0')\r\n",
    "    return df"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "def dataclean(file):\r\n",
    "    x = file.drop(['class'], axis = 1)\r\n",
    "    y = file['class']\r\n",
    "    return x,y"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "source": [
    "def model_accuracy(data, label):\r\n",
    "    X_train, X_test, y_train, y_test = train_test_split(data, label, test_size = 0.2, random_state = 20)\r\n",
    "    rfr = RandomForestRegressor(random_state = 42)\r\n",
    "    rfr.fit(X_train, y_train)\r\n",
    "    return rfr.score(X_train, y_train)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "source": [
    "def best(data,label):\r\n",
    "    X_train, X_test, y_train, y_test = train_test_split(data, label, test_size = 0.2, random_state = 20)\r\n",
    "    tree_param_grid = { 'min_samples_split': list(range(3,9,1)),'n_estimators':list(range(10,100,10))}\r\n",
    "    grid = GridSearchCV(RandomForestRegressor(),param_grid=tree_param_grid, cv=5)\r\n",
    "    grid.fit(X_train, y_train)\r\n",
    "    return grid.best_params_, grid.best_score_"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "source": [
    "def main(filename): \r\n",
    "    start_time = time.time()\r\n",
    "    data = dataload(filename)\r\n",
    "    X, y = dataclean(data)\r\n",
    "    accuracy = model_accuracy(X, y)\r\n",
    "    m, score_1 = best(X,y)\r\n",
    "    print(\"original accuracy is:\", score_1)\r\n",
    "    estimator = m['n_estimators']\r\n",
    "    min_sample = m['min_samples_split']\r\n",
    "    score_2 = RF(X, y, estimator, min_sample)\r\n",
    "    end_time = time.time()\r\n",
    "    print(\"after optimize, the accuracy is:\", score_2)\r\n",
    "    print(\"the accuracy is increased:\", score_2 - score_1)\r\n",
    "    return (end_time - start_time)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "source": [
    "def RF(data, label, estimators, min_sample):\r\n",
    "    data_train, data_test, label_train, label_test = train_test_split(data, label, train_size = 0.8, random_state = 42)\r\n",
    "    rfr = RandomForestClassifier(n_estimators = estimators, random_state = 42, min_samples_split = min_sample)\r\n",
    "    rfr.fit(data_train, label_train)\r\n",
    "    label_predict = rfr.predict(data_test)\r\n",
    "    label_predict = np.array(label_predict)\r\n",
    "    score = accuracy_score(label_predict.reshape(-1,1), np.array(label_test))\r\n",
    "    return score\r\n",
    "    "
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "source": [
    "res = main('Data/Phishing_Infogain_test.csv')"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "original accuracy is: 0.9253257320845609\n",
      "after optimize, the accuracy is: 0.9785296031229668\n",
      "the accuracy is increased: 0.05320387103840585\n",
      "time costs is: None\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}